package team.pwy.mapper;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import team.pwy.pojo.entity.Seckill;

import java.util.List;

@Repository
public interface SeckillMapper {

    @Select("select * from pwy.pwy_seckill order by star_time")
    List<Seckill> findAll();

    //活动开始之前商品id
    @Select("select * from pwy.pwy_seckill where star_time > CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status = 0")
    List<Seckill> findBeforeActiveIds();

    //活动进行中的秒杀的id
    @Select("select id from pwy.pwy_seckill where star_time <= CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and end_time > CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status = 1")
    List<Integer> findActiveIds();

    @Select("select * from pwy.pwy_seckill where star_time <= CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and end_time > CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status = 1")
    List<Seckill> findSeckillActive();

    //活动开始,状态 0 - 1
    @Update("update pwy.pwy_seckill set status = 1 where star_time <= CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and end_time > CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status=0")
    int updateActiveStatus();

    //活动结束,状态 1 - 2
    @Select("update pwy.pwy_seckill set status = 2 where  end_time <= CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status = 1")
    int updateEndStatus();

    //活动结束找出剩余商品的id
    @Select("select id from pwy.pwy_seckill where end_time >= CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai') and status = 2")
    List<Integer> findAfterActiveIds();

    @Update("UPDATE pwy.pwy_seckill SET star_time = DATE_ADD(CONVERT_TZ(NOW(), 'UTC', 'Asia/Shanghai'), INTERVAL 10 MINUTE)")
    void updateSecKillTime();

    @Select("select number from pwy.pwy_seckill where id = #{value} and status = 1")
    Integer findGoods(int sid);

    @Select("select * from pwy.pwy_seckill where id = #{value}")
    Integer findStatus(Integer id);
}
